package com.sctvcloud.zhaohua_sd.ui.activities;

import android.os.Build;
import android.os.Bundle;
import android.webkit.CookieManager;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;


import com.sctvcloud.zhaohua_sd.R;
import com.sctvcloud.zhaohua_sd.application.Constances;
import com.sctvcloud.zhaohua_sd.base.BaseDetailActivity;
import com.sctvcloud.zhaohua_sd.ui.utils.TitleUtils;

import butterknife.BindView;
import butterknife.ButterKnife;

public class UserAgreementActivity extends BaseDetailActivity {

    @BindView(R.id.webView_h5)
    protected WebView webView;

    private String url = Constances.ST_BASE_URL+"sctv/rule.html";

    private String privacyUrl = Constances.ST_BASE_URL+"sctv/logo/privacy.html";
    private String ruleUrl = Constances.ST_BASE_URL+"sctv/logo/rule.html";
    public static final String RULE_TYPE = "rule_type";
    private int ruleType =0;//0 用户协议，1 隐私政策

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ruleType = getIntent().getIntExtra(RULE_TYPE,0);
        if(ruleType==0) {
            new TitleUtils(this).setBackVisible(true).useAndSetTxTitle("用户协议");
        }else{
            new TitleUtils(this).setBackVisible(true).useAndSetTxTitle("隐私政策");
        }
        initView();
    }

    @Override
    protected void initInject() {
        setContentView(R.layout.activity_user_agreement);

        ButterKnife.bind(this);
    }

    private void initView() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            CookieManager.getInstance().setAcceptThirdPartyCookies(webView, true);
        }


        webView.setWebChromeClient(new WebChromeClient() {

        });
        webView.setWebViewClient(new WebViewClient() {
            /**
             * 防止加载网页时调起系统浏览器
             */
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                if (!url.startsWith("http://") && !url.startsWith("https://")) {
                    return true;
                }
                view.loadUrl(url);
                return true;
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                String titleStr = view.getTitle();
            }
        });

        initX5Setting();
        if(ruleType==0) {
            webView.loadUrl(ruleUrl);
        }else{
            webView.loadUrl(privacyUrl);
        }

    }

    private void initX5Setting() {
        WebSettings webSetting = webView.getSettings();
        String ua = webSetting.getUserAgentString();
        webSetting.setUserAgentString(ua + " SCGC_UA");
        webSetting.setCacheMode(WebSettings.LOAD_NO_CACHE);
        webSetting.setAllowFileAccess(true);
        webSetting.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
        webSetting.setSupportZoom(false);
        webSetting.setBuiltInZoomControls(false);
        webSetting.setUseWideViewPort(true);
        webSetting.setSupportMultipleWindows(false);
         webSetting.setLoadWithOverviewMode(true);
        webSetting.setAppCacheEnabled(true);
        // webSetting.setDatabaseEnabled(true);
        webSetting.setDomStorageEnabled(true);
        webSetting.setJavaScriptEnabled(true);
        webSetting.setGeolocationEnabled(true);
        webSetting.setAppCacheMaxSize(Long.MAX_VALUE);
        webSetting.setAppCachePath(this.getDir("appcache", 0).getPath());
        webSetting.setDatabasePath(this.getDir("databases", 0).getPath());
        webSetting.setGeolocationDatabasePath(this.getDir("geolocation", 0)
                .getPath());
        // webSetting.setPageCacheCapacity(IX5WebSettings.DEFAULT_CACHE_CAPACITY);
        webSetting.setPluginState(WebSettings.PluginState.ON_DEMAND);
        webView.setHorizontalScrollBarEnabled(false);
    }
}
